package intant.enums.test;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class SelectEnumTestCase {

    @Test
    public void testGetByCode() {
        // 通过code获取枚举实例，查找不到返回null
        Assertions.assertEquals(SelectEnum.getByCode(1, SampleSelectEnum.class), SampleSelectEnum.SUCCESS);
        Assertions.assertNull(SelectEnum.getByCode(99, SampleSelectEnum.class));
    }

    @Test
    public void testGetByCodeDef() {
        // 通过code获取枚举实例，设置默认值，如果查找不到返回默认值
        Assertions.assertEquals(SelectEnum.getByCode(2, SampleSelectEnum.class, SampleSelectEnum.SUCCESS), SampleSelectEnum.FAILURE);
        Assertions.assertEquals(SelectEnum.getByCode(99, SampleSelectEnum.class, SampleSelectEnum.SUCCESS), SampleSelectEnum.SUCCESS);
    }

    @Test
    public void testContains() {
        // 查看枚举中是否存在指定 code 编码的实例
        Assertions.assertTrue(SelectEnum.contains(1, SampleSelectEnum.class));
        Assertions.assertFalse(SelectEnum.contains(99, SampleSelectEnum.class));
    }

    @Test
    public void testGetDescByCode() {
        // 查看枚举中是指定 code 编码的实例描述，如果未找到实例返回 null
        Assertions.assertEquals(SelectEnum.getDescByCode(1, SampleSelectEnum.class), SampleSelectEnum.SUCCESS.getDesc());
        Assertions.assertNull(SelectEnum.getDescByCode(99, SampleSelectEnum.class));
    }

    @Test
    public void testGetDescByCodeDef() {
        // 查看枚举中是指定 code 编码的实例描述，给定默认值，如果未找到实例返回默认值
        Assertions.assertEquals(SelectEnum.getDescByCode(1, SampleSelectEnum.class, "ok"), SampleSelectEnum.SUCCESS.getDesc());
        Assertions.assertEquals(SelectEnum.getDescByCode(99, SampleSelectEnum.class, "ok"), "ok");
    }

    @Test
    public void testList() {
        Assertions.assertEquals(SelectEnum.list(SampleSelectEnum.class).size(), 2);
    }

    @Test
    public void testAsList() {
        Assertions.assertEquals(SelectEnum.asList(SampleSelectEnum.SUCCESS, SampleSelectEnum.FAILURE).size(), 2);
    }

    enum SampleSelectEnum implements SelectEnum<Integer> {
        SUCCESS(1, "成功"),
        FAILURE(2, "失败");

        private final Integer code;
        private final String desc;

        SampleSelectEnum(Integer code, String desc) {
            this.code = code;
            this.desc = desc;
        }

        @Override
        public Integer getCode() {
            return code;
        }

        @Override
        public String getDesc() {
            return desc;
        }

    }

}
